$.sct = {
    init:function(userID,myUserName){
        $(document).ready(function () {
    

            var socket = io.connect('http://leapdemo.com:3000');
            
            
            socket.emit('set username', myUserName,userID, function(data) {
                console.log('emit set username', data);
            });
            socket.emit('get users',myUserName,userID, function(data) {
                console.log('get users', data);
            });
            //	socket.on('entrance', function  (data) {
            //		alert(data.message);
            //	});
            //
            //	socket.on('exit', function  (data) {
            //                alert(data.message);
            //	});
       

            //                enableMsgInput(false);

            socket.on('userJoined', function(msg) {
                //appendNewUser(msg.userName, true);
                //alert(msg.userName+" Joined");
                $("#"+msg.userID).append("<span class='online-user'></span>");
            });
                
            socket.on('allUsers', function(msg) {
                   
                $.each(jQuery.parseJSON( msg.currentUsers ),function(key,value){
                    var user = jQuery.parseJSON(value);
                    if(user.id === userID){
                        return true;
                    }
                    $("#"+user.id).append("<span class='online-user'></span>");
                });
                    
            //appendNewUser(msg.userName, true);
            // $("#"+msg.userID).append("<span class='online-user'></span>");
            });
                
            socket.on('userLeft', function(msg) {
                //handleUserLeft(msg);
                var user = jQuery.parseJSON(msg.user)
                $("#"+user.id).find(".online-user").remove();
            });
                
                
            socket.on('notificate', function(data) {
                //handleUserLeft(msg);
                var answer = jQuery.parseJSON(data.msg)
                console.log(answer)
            });
            //                socket.on('message', function(msg) {
            //                    appendNewMessage(msg);
            //                });

            socket.on('welcome', function(msg) {
                var myUserName = '<?php if(isset ($username)) echo $username; ?>';
                socket.emit('set username', myUserName, function(data) {
                    console.log('emit set username', data);
                });
                console.log('Set user name as ' + myUserName);
            //setFeedback("<span style='color: green'> Username available. You can begin chatting.</span>");
            //                    setCurrentUsers(msg.currentUsers)
            //                    enableMsgInput(true);
            //                    enableUsernameField(false);
            });

            socket.on('error', function(msg) {
                if (msg.userNameInUse) {
                    setFeedback("<span style='color: red'> Username already in use. Try another name.</span>");
                }
            });
            
            
            $(".mainBlock").on('click',function(){
                var id_to = $(this).attr("id");
                var user_name = $("body").attr("data-screen-name");
                var action = "following";
                socket.emit('get notification', id_to,user_name,action, function(data) { 
                    console.log('emit set username', data); 
                });
            });
        //                $('input#userName').change(setUsername);
        //                $('input#userName').keypress(function(e) {
        //                    if (e.keyCode == 13) {
        //                        setUsername();
        //                        e.stopPropagation();
        //                        e.stopped = true;
        //                        e.preventDefault();
        //                    }
        //                });
        //
        //                $('input#msg').keypress(function(e) {
        //                    if (e.keyCode == 13) {
        //                        sendMessage();
        //                        e.stopPropagation();
        //                        e.stopped = true;
        //                        e.preventDefault();
        //                    }
        //                });
        });
    }
}


function setFeedback(fb) {
    $('span#feedback').html(fb);
}

function setUsername() {
    var myUserName = '<?php if(isset ($username)) echo $username; ?>';
    socket.emit('set username', myUserName, function(data) {
        console.log('emit set username', data);
    });
    console.log('Set user name as ' + myUserName);
}

function sendMessage() {
    var trgtUser = $('select#users').val();
    socket.emit('message',
    {
        "inferSrcUser": true,
        "source": "",
        "message": $('input#msg').val(),
        "target": trgtUser
    });
    $('input#msg').val("");
}

function setCurrentUsers(usersStr) {
    $('select#users >option').remove()
    appendNewUser('All', false)
    JSON.parse(usersStr).forEach(function(name) {
        appendNewUser(name, false);
    });
    $('select#users').val('All').attr('selected', true);
}
function enableMsgInput(enable) {
    $('input#msg').prop('disabled', !enable);
}

function enableUsernameField(enable) {
    $('input#userName').prop('disabled', !enable);
}

function appendNewMessage(msg) {
    var html;
    if (msg.target == "All") {
        html = "<span class='allMsg'>" + msg.source + " : " + msg.message + "</span><br/>"
    } else {
        // It is a private message to me
        html = "<span class='privMsg'>" + msg.source + " (P) : " + msg.message + "</span><br/>"
    }
    $('#msgWindow').append(html);
}

function appendNewUser(uName, notify) {
    $('select#users').append($('<option></option>').val(uName).html(uName));
    if (notify && (myUserName !== uName) && (myUserName !== 'All'))
        $('span#msgWindow').append("<span class='adminMsg'>==>" + uName + " just joined <==<br/>")
}

function handleUserLeft(msg) {
//$("select#users option[value='" + msg.userName + "']").remove();
}